home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Magazin: Amiga-CD 1997 July & August
/
Amiga-CD 1997 #7-8.iso
/
pd-disketten
/
dms-gepackt
/
4_96
/
apd-4-96-2.dms
/
apd-4-96-2.adf
/
Gut gekurvt
/
Listing.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-13
|
3KB
|
109 lines
#include <math.h>
#include <intuition/intuition.h>
#include <pragma/intuition_lib.h>
#include <pragma/exec_lib.h>
#include <pragma/graphics_lib.h>
#include <pragma/dos_lib.h>
#include <math.h>
#include <stream.h>
// die erste Version: schön, aber langsam
void Bezier1(struct RastPort *rp, double k0x,
double k0y,double k1x,double k1y,double k2x,
double k2y,double k3x,double k3y, double range)
{
// Vereinfachung für n=4; sonst ist die
const n = 4; // Parameterübergabe komplizierter
double bx[n+1][n+1]; // Zählung von 1-n
double by[n+1][n+1];
const double step = 1.0/range;
// Parameter in Array
bx[0][0]=k0x; bx[1][0]=k1x; bx[2][0]=k2x;
bx[3][0]=k3x; by[0][0]=k0y; by[1][0]=k1y;
by[2][0]=k2y; by[3][0]=k3y;
Move(rp, (long)k0x, (long)k0y);
for(double t = step; t <= range*step; t+= step) {
for(int r=1; r<=n; r++) {
for(int i=0; i<= n-r; i++) {
bx[i][r] = (1-t)*bx[i][r-1]+t*bx[i+1][r-1];
by[i][r] = (1-t)*by[i][r-1]+t*by[i+1][r-1];
}
}
Draw(rp, (long)bx[0][3], (long)by[0][3]);
}
}
void Bezier2(struct RastPort *rp,double k0x,
double k0y,double k1x,double k1y,double k2x,
double k2y,double k3x,double k3y, double range)
{
double bx, by;
const double step = 1.0/range;
// Konstante rst zum schnelleren Bearbeiten
const double rst = range*step;
Move(rp,(long)k0x,(long)k0y);
for(double t = step; t <= rst; t+= step)
{
bx = (1-t)*(1-t)*((1-t)*k0x + (3*t)*k1x)
+ (t*t)*(3*(1-t)*k2x + t*k3x);
by = (1-t)*(1-t)*((1-t)*k0y + (3*t)*k1y)
+ (t*t)*(3*(1-t)*k2y + t*k3y);
Draw(rp,(long)bx,(long)by);
}
}
void Bezier3(struct RastPort *rp,double k0x,
double k0y,double k1x,double k1y,double k2x,
double k2y,double k3x,double k3y, double range)
{
// Variablen, die nur einmal berechnet werden:
double bx, by, emt, emt2, t2, tm3, emtm3;
// improvisierte Abschätzung der Schrittweite:
const double step = 1.0/range;
const double rst = range*step;
Move(rp,(long)k0x,(long)k0y);
for(double t = step; t <= rst; t+= step)
{
emt = 1-t; emt2 = emt*emt; emtm3 = 3*emt;
t2 = t*t; tm3 = 3*t;
bx = emt2*(emt*k0x+tm3*k1x)+t2*(emtm3*k2x+t*k3x);
by = emt2*(emt*k0y+tm3*k1y)+t2*(emtm3*k2y+t*k3y);
Draw(rp,(long)bx,(long)by);
}
}
struct Window *win = OpenWindowTags
( NULL,
WA_Flags, WFLG_ACTIVATE | WFLG_CLOSEGADGET |
WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET,
WA_Top, 11, WA_Width, 640,
WA_Height, 512, WA_Left, 0,
WA_BlockPen, 4, WA_DetailPen, 3,
WA_Title, (char*)"Bézier-Kurven...", TAG_END
);
struct RastPort *rp = win->RPort;
#define F(x) SetAPen(rp,x)
void main()
{
if(win) { int i;
F(1); for (i=0; i< 50; i++)
Bezier1(rp,10,20+i,600,70+i, 20,70+i,600,160+i,80);
F(2); for (i=0; i< 50; i++)
Bezier2(rp,10,140+i,600,200+i,20,200+i,600,280+i,80);
F(3); for (i=0; i< 50; i++)
Bezier3(rp,10,250+i,600,320+i,20,320+i,600,430+i,80);
Delay(250); // 5 Sekunden warten
CloseWindow(win); // und schließen
}
}